/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uk.ac.cam.can.generalisator;

import java.util.Random;

/**
 *
 * @author Thomas
 */
public class GenCountry implements Generalisator{
    private String[] america = {"United-States", "Canada", "Outlying-US(Guam-USVI-etc)",  };
    private String[] latinAmerica = {"Columbia", "Puerto-Rico", "Honduras", "Ecuador", "Guatemala", "Cuba", "Jamaica", "Mexico",   "Nicaragua", "El-Salvador", "Peru"};
    private String[] asia = {"Cambodia", "India", "Japan", "China", "Iran", "Vietnam", "Laos", "Taiwan", "Thailand", "Hong"};
    private String[] europe = {"England", "Germany", "Greece", "Italy", "Poland", "Portugal", "Ireland", "France", "Hungary", "Scotland", "Yugoslavia", "Holand-Netherlands"};
    private String[] africa = {"South"};
    private String[] pacific = {"Philippines", "Dominican-Republic", "Haiti", "Trinadad&Tobago"};

    @Override
    public String gen(String value) {
        for(String target : america){
            if(value.contains(target))
                return "America";
        }
        for(String target : africa){
            if(value.contains(target))
                return "Africa";
        }
        for(String target : pacific){
            if(value.contains(target))
                return "Pacific";
        }
        for(String target : asia){
            if(value.contains(target))
                return "Asia";
        }
        for(String target : europe){
            if(value.contains(target))
                return "Europe";
        }
        for(String target : latinAmerica){
            if(value.contains(target))
                return "Latin America";
        }
        return "*";
    }
    
    @Override
    public int getMaxGeneralisation() {
        return 3;
    }

    @Override
    public double getLength(String value) {
        if(value.contains("*"))
            return 3.0;
        for(String target : america){
            if(value.contains(target))
                return 1.0;
        }
        for(String target : africa){
            if(value.contains(target))
                return 1.0;
        }
        for(String target : pacific){
            if(value.contains(target))
                return 1.0;
        }
        for(String target : asia){
            if(value.contains(target))
                return 1.0;
        }
        for(String target : europe){
            if(value.contains(target))
                return 1.0;
        }
        for(String target : latinAmerica){
            if(value.contains(target))
                return 1.0;
        }
        return 2.0;
    }

    @Override
    public double getMaxLength() {
        return 3.0;
    }

    @Override
    public String getUngeneralisation(String value) {
        Random rand = new Random(System.nanoTime());
        if(value.equals("*")){
            return "America";
        }
        if(value.equals("America")){
            return america[rand.nextInt(america.length)];
        }
        if(value.equals("Africa")){
            return africa[rand.nextInt(africa.length)];
        }
        if(value.equals("Pacific")){
            return pacific[rand.nextInt(pacific.length)];
        }
        if(value.equals("Asia")){
            return asia[rand.nextInt(asia.length)];
        }
        if(value.equals("Europe")){
            return europe[rand.nextInt(europe.length)];
        }
        if(value.equals("Latin America")){
            return latinAmerica[rand.nextInt(latinAmerica.length)];
        }
        return europe[rand.nextInt(europe.length)];
    }

    @Override
    public int getGeneralisationLevel(String value) {
        if(value.contains("*"))
            return 2;
        for(String target : america){
            if(value.contains(target))
                return 0;
        }
        for(String target : africa){
            if(value.contains(target))
                return 0;
        }
        for(String target : pacific){
            if(value.contains(target))
                return 0;
        }
        for(String target : asia){
            if(value.contains(target))
                return 0;
        }
        for(String target : europe){
            if(value.contains(target))
                return 0;
        }
        for(String target : latinAmerica){
            if(value.contains(target))
                return 0;
        }
        return 1;
    }
}
